package main.java.maziacs.counters;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;


//Se ha implementado un test unitario que verifica su corecto funcionamiento.

public class Bag {
	
		public Bag(){
			countersList=new LinkedList<Counter>();
			initializeBag();
		}
		/**
		 */
		public void initializeBag(){
			Random r = new Random();
			Integer valor;
			//Inicilizamos los contadores;
			Food f =  new Food(this);
			Prisoner p = new Prisoner(this);
			Sword s = new Sword(this);
			//Inicializamos valores:
			Integer contFood = f.getNumberCountersFood();
			Integer contSword = s.getNumberSwords();
			Integer contPrisoner = p.getNumberPrisioners();
			//Podemos tener Prisionero, Comida o Espada
			while(countersList.size()<20){
				valor = r.nextInt(4);
				//El random da un valor aleatorio enre 1 y 3
				if(valor ==1 && contFood>=1){
					this.countersList.add(f);
					contFood--;
				}else if(valor ==2 && contPrisoner>=1){
					this.countersList.add(p);
					contPrisoner--;
				}else if(valor==3 && contSword>=1){
					this.countersList.add(s);
					contSword--;
				}
			}
			if(this.countersList.size()!=20)
				throw new IllegalArgumentException("Se ha producido un error al rellenar la bolsa");
		}

			
				
		/*Extrae un contador de la bolsa*/
		public Counter extractCounter(){
			Random r=new Random();
			return countersList.remove(r.nextInt(countersList.size()));
		}




		/**
		 * @uml.property  name="countersList"
		 */
		private List<Counter> countersList;




		/**
		 * Getter of the property <tt>countersList</tt>
		 * @return  Returns the countersList.
		 * @uml.property  name="countersList"
		 */
		public List<Counter> getCountersList() {
			return countersList;
		}




		/**
		 * Setter of the property <tt>countersList</tt>
		 * @param countersList  The countersList to set.
		 * @uml.property  name="countersList"
		 */
		public void setCountersList(List<Counter> countersList) {
			this.countersList = countersList;
		}
		public String toString(){
			String cad= "La bolta contiene: \n";
			for(int i=0;i<20;i++)
				cad+=this.getCountersList().get(i).toString();
			return cad;
		}

}
